/**
*  fn_supplyDrop
*
*  Calls VTOL to drop a box with the action definend by _cargo attached
*
*  Domain: Server
* edit by 略游：可以重复同时使用
**/
params ["_targetPos", "_cargo", "_aircraft", "_damage"];

sleep 30;

private ["_angle", "_height", "_offsX", "_offsY", "_pointX", "_pointY", "_dropStart", "_dropTarget", "_dropEnd", "_agSpawn", "_agVehicle", "_agCrew", "_ag", "_agVehicle", "_reldir", "_waypoint0", "_waypoint1", "_parachute", "_supplyBox"];
_angle = round random 180;
_height = 300;
_offsX = 0;
_offsY = 1000;
_pointX = _offsX*(cos _angle) - _offsY*(sin _angle);
_pointY = _offsX*(sin _angle) + _offsY*(cos _angle);

_dropStart  = _targetPos vectorAdd [_pointX, _pointY, _height];
_dropTarget = [(_targetPos select 0), (_targetPos select 1), 100];
_dropEnd    = _targetPos vectorAdd [-_pointX*2, -_pointY*2, _height];;

_agSpawn = [_dropStart, 0, _aircraft, east] call bis_fnc_spawnvehicle;
_agVehicle = _agSpawn select 0;	//the aircraft
_agCrew = _agSpawn select 1;	//the units that make up the crew
_ag = _agSpawn select 2;	//the group
{_x allowFleeing 0} forEach units _ag;

_agVehicle flyInHeight 100;
_agVehicle setpos [getposATL _agVehicle select 0, getposATL _agVehicle select 1, _height];

_reldir = [_dropStart, _targetPos] call BIS_fnc_dirTo;
_agVehicle setdir _reldir;

(leader _ag) setVariable ["waypoint_end", false];

_waypoint0 = _ag addwaypoint[_dropTarget,0];
_waypoint0 setwaypointtype "Move";
_waypoint0 setWaypointCompletionRadius 5;
_waypoint0 setWaypointStatements ["true", "this setVariable ['waypoint_end', true];"];

_waypoint1 = _ag addwaypoint[_dropEnd,0];
_waypoint1 setwaypointtype "Move";

[_ag, 1] setWaypointSpeed "FULL";
[_ag, 1] setWaypointCombatMode "RED";
[_ag, 1] setWaypointBehaviour "CARELESS";

sleep 4;
_agVehicle animateDoor ['Door_1_source', 1];
waitUntil {(leader _ag) getVariable ["waypoint_end", true]};
sleep 1.5;

// Drop cargo
_parachute = "B_Parachute_02_F" CreateVehicle [0,0,0];
_parachute setPos [getPos _agVehicle select 0, getPos _agVehicle select 1, (getPos _agVehicle select 2)-5];


_supplyBox = createVehicle [_cargo, [0,0,0], [], 0, "CAN_COLLIDE"];
//迷彩
_supplyBox call fn_randomMask;

_supplyBox setDamage [_damage, false];
if(_damage != 0) then
{
	_supplyBox setFuel 0;
};
_supplyBox allowDamage false;

_supplyBox attachTo [_parachute, [0,0,-1]];

_supplyBox spawn {
waitUntil {isNull _this || {getpos _this select 2<4}};

//再等几秒防止损坏
sleep 5;

if (isNull _this) exitWith {};

//设置位置
private _pos_temp = getpos _this;
_pos_temp set [2, 0.1];

_this setVehiclePosition [_pos_temp, [], 0, "NONE"];
_this enableDynamicSimulation true;
//

_this allowDamage true;
private _smoker = "SmokeShellRed" createVehicle (getpos _this vectorAdd [0,0,5]);
detach _this;
};

sleep 50;//


deleteVehicle _agVehicle;
{deleteVehicle _x} foreach _agCrew;

deleteVehicle _parachute;//




